package com.ruoyi.project.mahjong.controller.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateRangeUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mahjong.domain.TIntegralExchange;
import com.ruoyi.project.mahjong.domain.TRecord;
import com.ruoyi.project.mahjong.domain.TShops;
import com.ruoyi.project.mahjong.domain.dto.DeskMemberDto;
import com.ruoyi.project.mahjong.domain.dto.MemberRechargeDto;
import com.ruoyi.project.mahjong.domain.enu.CostType;
import com.ruoyi.project.mahjong.domain.enu.ShopIncomeType;
import com.ruoyi.project.mahjong.domain.vo.*;
import com.ruoyi.project.mahjong.service.*;
import com.ruoyi.project.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 店铺流水相关操作
 */
@RestController
@RequestMapping("/mahjong/record")
@Api(tags = "店铺流水相关操作api")
public class RecordApiController extends BaseController {

    @Autowired
    private ITRecordService tRecordService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ITIntegralExchangeService itIntegralExchangeService;
    @Autowired
    private ITMemberService memberService;

    @Autowired
    private ITOpenMoneyService openMoneyService;

    @Autowired
    private ITOpenTableService openTableService;

    /**
     * 现金收入、其他收入(非现金) 收入总额、退款总额
     */
    @GetMapping("/Index")
    @ApiOperation("现金收入、扫码收入、其他收入(非现金)、收入总额、退款金额、其他支出、盈收")
    public Result<AllIncomeTypeMoneyVo> tableIndex(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                                                   @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        AllIncomeTypeMoneyVo res = tRecordService.selectRecordAllIncomeTypeMoney(shopId, startTime, endTime);
         return Result.success(res);
    }

    /**
     * 实现展示包房收入、公厅收入、商品收入、消额金额、赠送金额
     * @return
     */
    @GetMapping("/realTimeData")
    @ApiOperation("实时数据")
    public Result<ReceiveVo> realTimeData(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                               @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        ReceiveVo res=  tRecordService.selectRealTimeData(shopId,startTime,endTime);
        return Result.success(res);
    }
    @GetMapping("/tableCollect")
    @ApiOperation("桌台汇总")
    public Result<ReceiveVo> tableCollect(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                               @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        List<Map<String, Object>> list = tRecordService.selectTableCollectList(shopId, startTime, endTime);
        return Result.success(list);
    }

    /**
     * 经营流水 按照时间段来查询内容，走势图，按照现金收入，桌台消费
     * @return
     */
    @GetMapping("/AllRecord")
    @ApiOperation("经营流水")
    public Result<Map<String, Object>> AllRecord(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                               @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false)String endTime)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());

        Map<String, Object> objectMap = tRecordService.selectCashAndTableCost(shopId, startTime, endTime);
        return Result.success(objectMap);
    }

    /**
     * 赠送点数按钮
     * @return
     */
    @GetMapping("/giveMoney")
    @ApiOperation("赠送点数按钮")
    public Result giveMoney(@ApiParam("金额")@RequestParam(value = "money" ,required = true)String money,
                                                @ApiParam("会员id")@RequestParam(value = "id" ,required = true)String id)
    {
        return Result.success(memberService.updateGiveMoney(money,id)==0?"赠送失败":"成功赠送"+money+"点数");
    }


    /**
     * 充值记录 会员消费记录、消费时间、消费金额
     * @return
     */
    @GetMapping("/AllRecharge")
    @ApiOperation("充值记录")
    public TableDataInfo AllRecharge(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                                     @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime,
                                     @ApiParam("手机号")@RequestParam(value = "phone" ,required = false) String phone) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        Date date = DateUtils.parseDate(startTime);
        Date date2 = DateUtils.parseDate(endTime);
        startPage();
        List<MemberRechargeVo> res=  tRecordService.selectMemberIdRecharge(shopId,date,date2,phone);
        return getDataTable(res);
    }
    /**
     * 消费记录
     * @return
     */
    @GetMapping("/costRecord")
    @ApiOperation("消费记录")
    public Result<MemberRechargeVo> costRecord(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                                                @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime,
                                               @ApiParam("页数")@RequestParam(value = "page" ,required = false ,defaultValue = "0") int page,
                                               @ApiParam("条数")@RequestParam(value = "pageSize" ,required = false ,defaultValue = "10") int pageSize,
                                                @ApiParam("会员id")@RequestParam(value = "id" ,required = true) String id) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        PageHelper.startPage(page,pageSize);
        List<MemberCostRecordVo> res =  tRecordService.selectCostRecord(shopId,startTime,endTime,id);
        return Result.success(res);
    }
    /**
     * 兑换记录
     * @return
     */
    @GetMapping("/integralRecord")
    @ApiOperation("兑换记录")
    public Result<MemberRechargeVo> integralRecord(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                                               @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime,
                                               @ApiParam("页数")@RequestParam(value = "page" ,required = false ,defaultValue = "0") int page,
                                               @ApiParam("条数")@RequestParam(value = "pageSize" ,required = false ,defaultValue = "10") int pageSize,
                                               @ApiParam("会员id")@RequestParam(value = "id" ,required = true) String id) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        PageHelper.startPage(page,pageSize);
        List<TIntegralExchange> res =  itIntegralExchangeService.selectTIntegralExchangeByIdAndTime(shopId,startTime,endTime,id);
        return Result.success(res);
    }
    @PostMapping("/saveIntegralCover")
    @ApiOperation("积分兑换")
    public Result<MemberRechargeVo> integralCover(@RequestBody MemberRechargeDto dto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
       itIntegralExchangeService.integralCover( dto,shopId,user);
        return Result.success();
    }
    /**
     * 会员充值排行
     * @return
     */
    @GetMapping("/rechargeRanking")
    @ApiOperation("会员充值和消费排行")
    public Result<MemberRechargeVo> rechargeRanking(@ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
                                                   @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false) String endTime,
                                                   @ApiParam("页数")@RequestParam(value = "page" ,required = false ,defaultValue = "0") int page,
                                                   @ApiParam("条数")@RequestParam(value = "pageSize" ,required = false ,defaultValue = "10") int pageSize,
                                                   @ApiParam("查询状态 1:查询充值排行 2:查询消费排行")@RequestParam(value = "status" ,required = true,defaultValue = "2") String status
    ) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        PageHelper.startPage(page,pageSize);
        List<RechargeRankingVo> res =  tRecordService.selectMemberRechargeList(shopId,startTime,endTime,status);
        return Result.success(res);
    }



    @GetMapping("/delOpenDeskMember")
    @ApiOperation(value = "删除开桌人员信息",httpMethod = "GET")
    public AjaxResult delOpenDeskMember(@Validated DeskMemberDto deskMemberDto){
        openTableService.deleteByMemberId(deskMemberDto);
        return  AjaxResult.success();
    }

    /**
     * 充值或退款列表
     * @return
     */
    @GetMapping("/topUpOrRefund")
    @ApiOperation("充值退款列表")
    public TableDataInfo TopUpOrRefund(
            @ApiParam("类型")@RequestParam(value = "type" ,required = false)String type,
            @ApiParam("桌子")@RequestParam(value = "deskId" ,required = false)String deskId,
            @ApiParam("桌子")@RequestParam(value = "staffName" ,required = false)String staffName,
            @ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
            @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false)String endTime,
            @ApiParam("页数")@RequestParam(value = "pageNum" ,required = false ,defaultValue = "0")int pageNum,
            @ApiParam("条数")@RequestParam(value = "pageSize" ,required = false ,defaultValue = "10") int pageSize
    ) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        List<TRecord> tRecords = tRecordService.selectTopUpOrRefundList(shopId, type, startTime, endTime,deskId,staffName);
        return getDataTable(tRecords);
    }



    /**
     * 充值或退款列表
     * @return
     */
    @GetMapping("/deskMoneyList")
    @ApiOperation("桌子流水")
    public TableDataInfo desk(
            @ApiParam("类型")@RequestParam(value = "type" ,required = false)String type,
            @ApiParam("桌子")@RequestParam(value = "deskId" ,required = false)String deskId,
            @ApiParam("姓名")@RequestParam(value = "staffName" ,required = false)String staffName,
            @ApiParam("开始日期")@RequestParam(value = "startTime" ,required = false)String startTime,
            @ApiParam("结束日期")@RequestParam(value = "endTime" ,required = false)String endTime
    ) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        List<TRecord> tRecords = tRecordService.deskMoneyList(shopId, startTime, endTime,deskId,staffName);
        return getDataTable(tRecords);
    }
}
